define(function(require, exports) {
	var $ = require('jquery');
	var pubjs = require('pubjs');
	var util = require('util');
	var view  = require('@base/view');
	var input = require('@base/common/input');

	var Lbs = view.container.extend({
		init: function (config) {
			config = pubjs.conf(config, {
				view_model:
				{
					'eventAddButton': this.eventAddButton
				}
			});
			this.Super('init', arguments);

			this.$count = 0;
		},
		afterBuild: function () {

			var el = this.getDOM();

			var doms = this.doms = {
				con: $('<div class="M-formHotWord"/>').appendTo(el)
			};
			doms.itemCon = $('<div class="M-formHotWordCon">').appendTo(doms.con);
			this.buildItem(this.$count);

			$([
				'<div class="mb10 mt10">',
					LANG('百度地图坐标拾取工具地址：'),
					'<a href="http://api.map.baidu.com/lbsapi/getpoint/index.html" target="_blank">',
					'http://api.map.baidu.com/lbsapi/getpoint/index.html',
					'</a>',
				'</div>',
				'<button ms-click="eventAddButton" class="btn btn-default">'+LANG('添加坐标')+'</button>'
			].join('')).appendTo(doms.con);

			this.uiProxy(el, '.M-PageHotWordConItemBtnDel', 'click', 'eventDelItem');

		},
		buildItem: function (id) {
			var self = this;
			var con = self.doms.itemCon;
			id = id || 0;

			// item容器
			var wrap = $('<div class="M-PageHotWordConItem P-campaignEditBox pt20 pb20" data-id="' + id + '"/>').appendTo(con);

			// 其他内容区
			wrap.append($('<span class="mr10"/>').html(LANG('对坐标点')));

			self.create('coord' + id, input.text, {
				'label': '',
				'target': wrap,
				'width': 200,
				'height': 30,
				'value': '',
				'disabled': false,
				'placeholder': LANG('经度, 纬度')
			});

			wrap.append($('<span class="mg10"/>').html(LANG('在')));


			self.create('radius' + id, input.text, {
				'label': '',
				'target': wrap,
				'width': 80,
				'height': 30,
				'value': "",
				'placeholder': '0'
			});

			wrap.append($('<span class="mg10"/>').html(LANG('米为半径的范围内的设备进行定向')));

			if (id)
			{
				//按钮-删除item
				$('<span class="M-PageHotWordConItemBtnDel" data-id="' + id + '"></span>').appendTo(wrap);
			}
		},
		eventDelItem: function (evt, elm) {
			var cs = this.$;
			var id = $(elm).attr('data-id');

			// 销毁模块
			cs['coord' + id].destroy();
			cs['radius' + id].destroy();

			// 移除元素
			$(elm).parent().remove();

			return false;
		},
		eventAddButton: function ()
		{
			var self = this;
			// 查找有多少个坐标组
			var length = self.doms.itemCon.find('.M-formHotWordConItem:visible').length;
			if (length >= 5)
			{
				pubjs.alert(LANG('最多填写5个坐标点'));
				return false;
			}
			this.buildItem(++this.$count);
		},
		setValue: function (data)
		{
			var self = this;
			var cs = self.$;

			if (data && data.length > 0)
			{
				util.each(data, function(item, idx)
				{
					if (idx > 0)
					{
						self.buildItem(idx);
						self.$count = idx;
					}
					if (item)
					{
						cs['coord' + idx].setValue([item.Lng, item.Lat].join(','));
						cs['radius' + idx].setValue(item.Radius);
					}
				});

			}
		},
		getValue: function ()
		{
			var self = this;
			var doms = self.doms;
			var data = [];
			var cs = self.$;

			for (var i = 0; i <= self.$count; i++)
			{
				var itemEl = doms.itemCon.find('.M-PageHotWordConItem[data-id=' + i + ']:visible');
				if (itemEl && itemEl.length)
				{
					var coord = [];
					var lng = 0; // 经度
					var lat = 0; // 纬度
					var radius = 0; // 半径
					if (cs['coord' + i])
					{
						coord = cs['coord' + i].getValue().split(/,|，/g);
					}
					if (coord.length == 2)
					{
						lng = util.trim(coord[0]);
						lat = util.trim(coord[1]);
					}
					if (cs['radius' + i])
					{
						radius = cs['radius' + i].getValue();
					}
					data.push({
						Lng: +lng || 0,
						Lat: +lat || 0,
						Radius: +radius || 0
					});
				}
			}
			return data;
		},
		reset: function ()
		{
			var cs = this.$;
			// 销毁所有模块
			for (var i = 0; i <= this.$count; i++) {
				if(cs['coord' + i]){
					cs['coord' + i].destroy();
				}
				if(cs['radius' + i]){
					cs['radius' + i].destroy();
				}
			}
			// 清空元素，再构建
			this.doms.itemCon.empty();
			this.buildItem(0);
			this.$count = 0;
		}
	});
	exports.base = Lbs;


});